package com.tul.vapp.servicecenter.controller;

import com.alibaba.fastjson.JSONObject;
import com.tul.cusa.common.util.StringUtil;
import com.tul.cusa.container.jetty.MQBroker;
import com.tul.vapp.servicecenter.bean.ResponseBean;
import com.tul.vapp.servicecenter.bean.VappCmdBean;
import com.tul.vapp.servicecenter.common.SysConfigConstant;
import com.tul.vapp.servicecenter.entity.AppVo;
import com.tul.vapp.servicecenter.entity.ConfigBeanVo;
import com.tul.vapp.servicecenter.entity.ItemVo;
import com.tul.vapp.servicecenter.entity.Namespace;
import com.tul.vapp.servicecenter.service.IAppService;
import com.tul.vapp.servicecenter.service.IConfigService;
import com.tul.vapp.servicecenter.service.IItemService;
import com.tul.vapp.servicecenter.service.IManager;
import com.tul.vapp.servicecenter.service.INamespaceService;
import com.tul.vapp.servicecenter.util.SysConfigUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * create by wujunlei 处理导入导出文件
 *
 * @author wujunlei
 * @since 2018/1/5
 */
@RestController
public class ConfigController {

    /**
     * mq
     */
    private MQBroker mqBroker = new MQBroker(SysConfigUtil.getProperty(SysConfigConstant.MQ_URL));

    /**
     * config service
     */
    @Autowired
    private IConfigService service;

    /**
     * appservice
     */
    @Autowired
    private IAppService appService;

    /**
     * namespaceService
     */
    @Autowired
    private INamespaceService namespaceService;

    /**
     * itemService
     */
    @Autowired
    private IItemService itemService;

    /**
     * manager
     */
    @Autowired
    private IManager manager;

    /**
     * 导出当前配置数据
     *
     * @return ResponseBean
     */
    @RequestMapping(value = "/exportConifgData")
    public ResponseBean exportConifgData() {
        ResponseBean bean = new ResponseBean();
        String filePath = service.exportConifgData();
        if (StringUtil.isEmpty(filePath)) {
            bean.setSuccess(false);
        } else {
            bean.setSuccess(true);
            bean.setData(filePath);
        }
        return bean;
    }

    /**
     * 导入已发布配置数据
     *
     * @param configBeanVos 配置对象
     * @return bean
     */
    @RequestMapping(value = "/importConifgData")
    public ResponseBean importConifgData(@RequestBody List<ConfigBeanVo> configBeanVos) {
        ResponseBean bean = new ResponseBean();

        if (service.importConifgData(configBeanVos)) {
            bean.setSuccess(true);
        } else {
            bean.setSuccess(false);
        }

        return bean;
    }

    /**
     * 获取全量应用列表
     *
     * @return 全量应用列表
     */
    @RequestMapping(value = "/getApps")
    public ResponseBean getApps() {
        ResponseBean bean = new ResponseBean();
        List<AppVo> apps = appService.getAppVos();
        for (AppVo appVo : apps) {
            if (appVo.getAppId().equals("ems-proxy")) {
                List<Namespace> namespaces = appVo.getParams();
                Iterator iterator = namespaces.iterator();
                while (iterator.hasNext()) {
                    Namespace namespace = (Namespace) iterator.next();
                    if (!namespace.getNamespace().equals("config.properties")) {
                        iterator.remove();
                    }
                }

            }
        }
        bean.setData(apps);
        return bean;
    }

    /**
     * 获取配置项
     *
     * @param params 入参
     * @return 配置项信息
     */
    @RequestMapping(value = "/getItems")
    public ResponseBean getItems(@RequestBody Map params) {
        String appId = (String) params.get("appId");
        String namespace = (String) params.get("namespace");
        ResponseBean bean = new ResponseBean();
        List<ItemVo> items = itemService.getItemVos(appId, namespace);
        bean.setData(items);
        return bean;
    }

    /**
     * 更新配置项的值
     *
     * @param itemVo item
     * @return 是否更新成功
     */
    @RequestMapping(value = "/updateItemValue")
    public ResponseBean updateItemValue(@RequestBody ItemVo itemVo) {
        ResponseBean bean = new ResponseBean();
        itemService.update(itemVo);
        bean.setSuccess(true);
        return bean;
    }

    /**
     * 发布
     *
     * @param params 入参
     * @return ResponseBean
     */
    @RequestMapping("/publishConfig")
    public ResponseBean publishConfig(@RequestBody Map params) {
        String appId = (String) params.get("appId");
        String namespace = (String) params.get("namespace");
        ResponseBean bean = new ResponseBean();
        manager.publish(appId, namespace);
        bean.setSuccess(true);
        return bean;
    }

    /**
     * 一键发布
     *
     * @return ResponseBean
     */
    @RequestMapping("/publishAll")
    public ResponseBean publishAll() {
        ResponseBean bean = new ResponseBean();
        manager.publish();
        bean.setSuccess(true);
        return bean;
    }

    /**
     * 发送指令
     *
     * @param cmd cmd
     * @return ResponseBean
     */
    @RequestMapping("/sendVappCmd")
    public ResponseBean sendVappCmd(VappCmdBean cmd) {
        ResponseBean bean = new ResponseBean();
        String mqMessage = JSONObject.toJSONString(cmd);
        mqBroker.sendTopic("vapp_cmd", mqMessage);
        bean.setSuccess(true);
        return bean;
    }
}
